﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using bdxyit.com.RecruitmentAppSys.IDAL;
using bdxyit.com.RecruitmentAppSys.Models;
using bdxyit.com.RecruitmentAppSys.DBUtility;
using System.Data.SqlClient;
using System.Data;
/***
*机器名：BDXYIT-HUI
*项目名称：$projectname$
*命名空间：bdxyit.com.RecruitmentAppSys.SQLDAL
*文件名：DegreeListModels
*系统时间：3/25/2012 3:34:37 PM
*当前用户：huihui
*创建时间：2012
*
*修改时间：2012 3/25/2012 3:34:37 PM
*修改事件：
***/

namespace bdxyit.com.RecruitmentAppSys.SQLDAL
{
    public class DegreeListDAL : IDegreeListDAL
    {
        private const string SQL_SELECT_DEGREELIST = " select  DegreeID,DegreeName from DegreeLists ";
        private const string SQL_SELECT_DEGREELIST_BY_DEGREEID = " select  top 1 DegreeID,DegreeName from DegreeLists "
                                                                + "  where DegreeID=@DegreeID";
        private const string SQL_DELETE_DEGREELIST_BY_DEGREEID = " delete from DegreeLists "
                                                                  + "  where DegreeID=@DegreeID";
        private const string SQL_UPDATE_DEGREELIST_BY_DEGREEID = " update DegreeLists set "
                                                                + " DegreeID=@DegreeID,"
                                                                + " DegreeName=@DegreeName"
                                                                + "  where DegreeID=@DegreeID";
        private const string SQL_INSERT_DEGREELIST = " insert into DegreeLists("
                                                    + " DegreeID,DegreeName)"
                                                    + "  values ("
                                                    + " @DegreeID,@DegreeName)";
        private const string FIELD_DegreeID = "@DegreeID";
        private const string FIELD_DegreeName = "@DegreeName";
        public bool InsertDegreeList(DegreeList cmodel)
        {
            bool flag = false;
            SqlParameter[] DegreeListParms = GetDegreeListParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句
            DegreeListParms[0].Value = cmodel.DegreeID;
            DegreeListParms[1].Value = cmodel.DegreeName;

            foreach (SqlParameter parm in DegreeListParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_INSERT_DEGREELIST;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool DeleteDegreeList(string DegreeID)
        {
            bool flag = false;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_DegreeID, SqlDbType.VarChar, 10);
            parm.Value = DegreeID;
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_DELETE_DEGREELIST_BY_DEGREEID;
                cmd.Parameters.Add(parm);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool UpdateDegreeList(DegreeList cmodel)
        {
            bool flag = false;
            SqlParameter[] DegreeListParms = GetDegreeListParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句

            DegreeListParms[0].Value = cmodel.DegreeID;
            DegreeListParms[1].Value = cmodel.DegreeName;
            foreach (SqlParameter parm in DegreeListParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_UPDATE_DEGREELIST_BY_DEGREEID;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public DegreeList SelectDegreeListByDegreeID(string DegreeID)
        {
            DegreeList cmodel = null;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_DegreeID, SqlDbType.VarChar, 10);
            parm.Value = DegreeID;
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_DEGREELIST_BY_DEGREEID, parm))
            {
                if (dr.Read())
                {
                    cmodel = new DegreeList();
                    if (dr.HasRows == true)
                    {
                        if (dr["DegreeID"] != null && dr["DegreeID"].ToString() != "")
                        {
                            cmodel.DegreeID = dr["DegreeID"].ToString();
                        }
                        if (dr["DegreeName"] != null && dr["DegreeName"].ToString() != "")
                        {
                            cmodel.DegreeName = dr["DegreeName"].ToString();
                        }
                    }
                }
            }
            return cmodel;
        }
        public List<DegreeList> SelectDegreeList()
        {
            List<DegreeList> selectlist = new List<DegreeList>();
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_DEGREELIST))
            {
                while (dr.Read())
                {
                    DegreeList cmodel = new DegreeList();
                    //写添赋值语句
                    if (dr["DegreeID"] != null && dr["DegreeID"].ToString() != "")
                    {
                        cmodel.DegreeID = dr["DegreeID"].ToString();
                    }
                    if (dr["DegreeName"] != null && dr["DegreeName"].ToString() != "")
                    {
                        cmodel.DegreeName = dr["DegreeName"].ToString();
                    }
                    selectlist.Add(cmodel);
                }
            }
            return selectlist;
        }
        private SqlParameter[] GetDegreeListParameters()
        {
            SqlParameter[] parms = new SqlParameter[] {
				    new SqlParameter(FIELD_DegreeID, SqlDbType.VarChar,10),
					new SqlParameter(FIELD_DegreeName, SqlDbType.VarChar,50)};
            return parms;
        }

    }
}